package cn.com.yinhex.as.util;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Awt
{
	private BufferedImage image = null;

	public static final int FILE = 1;
	public static final int URL = 2;
	public static final String JPG = "jpg";
	public static final String PNG = "png";
	public static final int FIXWIDTH = 1;
	public static final int FIXHEIGHT = 2;
	public static final int AUTOSIZE = 3;

	/**
	 * ��ʼ��
	 */
	public void init()
	{}

	public Awt()
	{}

	/**
	 * �Ӵ��̶�ȡһ��ͼ���ļ�
	 * 
	 * @param filename
	 *            �ļ���
	 * @param type
	 *            ���ͣ�
	 *            <li>1->�����ļ�
	 *            <li>2->Զ���ļ�
	 *            <li>3->������
	 */
	public void read(String filename, int type) throws IOException
	{
		switch (type)
		{
			case 1 :
				File file = new File(filename);
				image = ImageIO.read(file);
				break;
			case 2 :
				URL url = new URL(filename);
				image = ImageIO.read(url);
				break;
		}
	}

	/**
	 * ��ͼ��д�����
	 * 
	 * @param filename
	 *            �ļ���
	 * @param type
	 *            �������
	 *            <li>jpeg
	 *            <li>png
	 *            <li>gif
	 */
	public void write(String filename, String type) throws IOException
	{
		// Save as PNG or JPEG
		if (type == "jpg" || type == "png")
		{
			File file = new File(filename);
			ImageIO.write(image, type, file);
		}
	}

	/**
	 * ��ͼ�������ҳ
	 * 
	 * @param res
	 *            ��������
	 */
	public void output(OutputStream out) throws Exception
	{
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image);
		out.flush();
		out.close();
	}

	/**
	 * �������ͼ
	 * 
	 * @param w
	 *            �����
	 * @param h
	 *            ���߶�
	 * @param type
	 *            ������ͣ�1->ƥ���ȣ�2->ƥ��߶ȣ�3->���ƴ�С
	 */
	public void little(int w, int h, int type)
	{
		double width = w;
		double height = h;
		double scale = (double) image.getWidth(null) / (double) image.getHeight(null);
		switch (type)
		{
			case 1 :
			{
				height = width / scale;
				break;
			}
			case 2 :
			{
				width = height * scale;
				break;
			}
			case 3 :
			{
				if (width / height > scale)
				{
					width = height * scale;
				}
				else if ((width / height) < scale)
				{
					height = width / scale;
				}
				break;
			}
		}
		BufferedImage bimage = new BufferedImage((int) width, (int) height,
				BufferedImage.TYPE_INT_RGB);
		bimage.getGraphics().drawImage(image, 0, 0, (int) width, (int) height, null);
		this.image = bimage;
	}

	public int getWidth()
	{
		return image.getWidth();
	}

	public int getHeight()
	{
		return image.getHeight();
	}

	///PRIVATE///
	// FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * ����ҳ��ɫ�������Color����
	 * 
	 * @param color
	 *            ��ҳ��ɫ����
	 * @return Color����
	 */
	public static Color getColor(String s)
	{
		int val = 0;
		try
		{
			if (s.startsWith("0x"))
				val = Integer.parseInt(s.substring(2), 16);
			else if (s.startsWith("#"))
				val = Integer.parseInt(s.substring(1), 16);
			else if (s.startsWith("0") && s.length() > 1)
				val = Integer.parseInt(s.substring(1), 8);
			else
				val = Integer.parseInt(s, 10);
			return new Color(val);
		}
		catch (NumberFormatException numberformatexception)
		{
			return null;
		}
	}
}